package com.vcg.vdam.testbeandefinitioHttpApiInvoke;

import com.alibaba.fastjson.JSONObject;
import com.vcg.vdam.annotation.YangxingAnnotation;
import com.vcg.vdam.annotation.YangxingScanAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;


/**
 * Scanning the base packages that {@link HttpApiScan} specified.
 *
 * @author huangxuyang
 * date 2018/10/31
 */
@Component
public class HttpApiConfigurer implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
    private static final Logger logger = LoggerFactory.getLogger(HttpApiConfigurer.class);
    private static final String CLASSPATH_PRE = "classpath:";
    private static final String FILE_PRE = "file:";
    private ApplicationContext ctx;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {

        Map<String, Object> beans = ctx.getBeansWithAnnotation(YangxingScanAnnotation.class);
        //可以在这获取到一些对象；虽然还没初始化；可以把某些对象放到代理中；这个就相当于mybatis的动态代理很像；
        //里面包含sqlsession,sqlsessiontemplate等等
        //Object myController = ctx.getBean("myController");
        //System.out.println(JSONObject.toJSONString(myController));
        String basepackages = "";
        for (Map.Entry<String, Object> entry : beans.entrySet()) {
            YangxingScanAnnotation ann = entry.getValue().getClass().getAnnotation(YangxingScanAnnotation.class);
            basepackages = ann.value();
        }
        ClassPathHttpApiScanner classPathHttpApiScanner = new ClassPathHttpApiScanner(beanDefinitionRegistry);
        Set<BeanDefinitionHolder> beanDefinitionHolders = classPathHttpApiScanner.doScan(basepackages);


        for(BeanDefinitionHolder beanDefinition :beanDefinitionHolders){
            GenericBeanDefinition beanDefinition1 = (GenericBeanDefinition)beanDefinition.getBeanDefinition();
            //todo 可以获取到注解上的参数；以后应该可以用
            getAnnotationValue(beanDefinition);

            beanDefinition1.getPropertyValues().add("interfaceClass", beanDefinition1.getBeanClassName());
            beanDefinition1.setBeanClass(HttpApiProxyFactoryBean.class);
            beanDefinitionRegistry.registerBeanDefinition(beanDefinition.getBeanName(),beanDefinition1);
        }


    }

    private void getAnnotationValue(BeanDefinitionHolder beanDefinition) {
        AnnotatedBeanDefinition beanDefinition2 = (AnnotatedBeanDefinition)beanDefinition.getBeanDefinition();
        Map<String, Object> annotationAttributes = beanDefinition2.getMetadata().getAnnotationAttributes("com.vcg.vdam.annotation.YangxingAnnotation");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws
            BeansException {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ctx = applicationContext;
    }
}
